import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import utility.NetUtils;
import java.awt.Choice;
import java.util.List;

public class SettingsFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JTextField textFieldPort;
	private JTextField textFieldConnectionKey;

	/**
	 * Create the frame.
	 */
	public SettingsFrame(ServerTray tray, int currentPort, String connectionKey) {

		List<String> ipAddresses = NetUtils.getLocalIPAddresses();

		setResizable(false);
		// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setTitle("TouchMote\u2122: Settings");

		setBounds(100, 100, 303, 181);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("Your IP Address:");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(13, 21, 108, 16);
		contentPane.add(lblNewLabel);

		JLabel lblPort = new JLabel("Port:");
		lblPort.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPort.setBounds(43, 48, 78, 16);
		contentPane.add(lblPort);

		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new SaveListener(tray));
		btnSave.setBounds(180, 120, 93, 29);
		contentPane.add(btnSave);

		JButton btnCancel = new JButton("Cancel");

		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnCancel.setBounds(70, 120, 93, 29);
		contentPane.add(btnCancel);

		textFieldPort = new JTextField();
		textFieldPort.setColumns(10);
		textFieldPort.setBounds(139, 46, 134, 20);
		textFieldPort.setText(currentPort + "");
		contentPane.add(textFieldPort);

		JLabel lblConnectionKey = new JLabel("Connection Key:");
		lblConnectionKey.setHorizontalAlignment(SwingConstants.RIGHT);
		lblConnectionKey.setBounds(13, 75, 108, 16);
		contentPane.add(lblConnectionKey);

		textFieldConnectionKey = new JTextField();
		textFieldConnectionKey.setEditable(false);
		textFieldConnectionKey.setColumns(10);
		textFieldConnectionKey.setBounds(139, 73, 134, 20);
		textFieldConnectionKey.setText(connectionKey);
		contentPane.add(textFieldConnectionKey);

		Choice ipChoice = new Choice();
		ipChoice.setBounds(139, 17, 134, 28);
		contentPane.add(ipChoice);
		for (String address : ipAddresses) {
			ipChoice.add(address);
		}
		this.setLocationRelativeTo(null);

		// Imposta la finestra come finestra attiva
		this.setAlwaysOnTop(true);

	}

	private class SaveListener implements ActionListener {
		private ServerTray tray;

		public SaveListener(ServerTray tray) {
			this.tray = tray;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (textFieldPort.getText() != "")
				tray.setPort(Integer.parseInt(textFieldPort.getText()));
			dispose();

		}

	}
}
